﻿namespace BM.Tools.WebControls.MForm.Controls.Modifiers
{
    using System;
    using System.ComponentModel;

    using BM.Tools.WebControls.MForm.Controls.Items;

    /// <summary>
    /// A modifier that changes the display of the control, to look like a section.    
    /// </summary>
    public class SectionDisplayModifier : OffspringsOwnerModifier
    {
        private const string SectionCollapsedCssClass = "SC";

        /// <summary>
        /// Initializes a new instance of the <see cref="SectionDisplayModifier"/> class.
        /// </summary>
        public SectionDisplayModifier()
        {
            TitleVisible = true;
            HasMinimizing = true;
        }

        /// <summary>
        /// Gets or sets a value indicating whether HasMinimizing.
        /// </summary>
        /// <value>
        /// The has minimizing.
        /// </value>
        [DefaultValue(true)]
        public bool HasMinimizing
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets a value indicating whether IsCollapsed.
        /// </summary>
        /// <value>
        /// The is collapsed.
        /// </value>
        [DefaultValue(false)]
        public bool IsCollapsed
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets a value indicating whether TitleVisible.
        /// </summary>
        /// <value>
        /// The title visible.
        /// </value>
        [DefaultValue(true)]
        public bool TitleVisible
        {
            get;
            set;
        }

        /// <summary>
        /// Performs changes to the offspring item so that it is initially collapsed.
        /// </summary>
        /// <param name="offspringItem">
        /// The offspring item.
        /// </param>
        public static void CollapseOffspring(OffspringsOwnerItem offspringItem)
        {
            offspringItem.CssClasses.Add(SectionCollapsedCssClass);
            offspringItem.Title.MinimizeButton.Collapsed = true;
        }

        /// <summary>
        /// Performs changes to the offspring item so that it is initially expanded.
        /// </summary>
        /// <param name="offspringItem">
        /// The offspring item.
        /// </param>
        public static void ExpandOffspring(OffspringsOwnerItem offspringItem)
        {
            offspringItem.CssClasses.Remove(SectionCollapsedCssClass);
            offspringItem.Title.MinimizeButton.Collapsed = false;
        }

        /// <summary>
        /// Sets the title properties for each item
        /// </summary>
        /// <param name="offspringItem">
        /// The offspring item.
        /// </param>
        /// <param name="args">
        /// The event args.
        /// </param>
        protected override void OnItemPreRender(OffspringsOwnerItem offspringItem, EventArgs args)
        {
            offspringItem.Title.Visible = TitleVisible;
            if (!TitleVisible)
            {
                return;
            }

            offspringItem.Title.MinimizeButton.Visible = HasMinimizing;
            if (IsCollapsed)
            {
                CollapseOffspring(offspringItem);
            }
            else
            {
                ExpandOffspring(offspringItem);
            }
        }
    }
}